package com.yjx.config;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class WriteToTxtFile {
    public static void main(String[] args) {
        String xlsxFilePath = "C:\\Users\\zhou\\Desktop\\一级圆柱齿轮减速器有V带.xlsx";
        String txtFilePath = "C\\Users\\zhou\\Desktop\\file.txt";
        Map<String, Integer> keyValueMap = new HashMap<>();

        try {
            FileInputStream file = new FileInputStream(new File(xlsxFilePath));
            XSSFWorkbook workbook = new XSSFWorkbook(file);
            XSSFSheet sheet = workbook.getSheetAt(1); // 获取第二章工作表
            int lastRowNum = sheet.getLastRowNum();

            for (int i = 1; i <= lastRowNum; i++) { // 从第二行开始遍历
                XSSFRow row = sheet.getRow(i);
                XSSFCell cellB = row.getCell(1); // 获取B列单元格
                XSSFCell cellC = row.getCell(2); // 获取C列单元格

                if (cellB != null && cellC != null) {
                    String key = cellB.getStringCellValue();
                    int value = (int) cellC.getNumericCellValue();
                    keyValueMap.put(key, value);
                }
            }

            FileWriter writer = new FileWriter(txtFilePath);
            for (Map.Entry<String, Integer> entry : keyValueMap.entrySet()) {
                String key = entry.getKey();
                int value = entry.getValue();
                writer.write("\"" + key + "\"= " + value + "\n");
            }
            writer.close();

            System.out.println("Writing to txt file completed successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
